package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.aliyun.gts.bpaas.middleware.mq.api.model.MqPayload;
import com.fasterxml.jackson.databind.JsonNode;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.FlowElementProcessContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.MqMessageContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.ProcessItem;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.MqProcessTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CustomPropertiesProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * @author: lzb
 * @date: 2021/11/18  17:04
 */
@Slf4j
@Service
public class FlowDeployMessageHandler implements CustomPropertiesProcessor {

    @Override
    public boolean support(JsonNode elementNode) {
        JsonNode properties = elementNode.get("properties");
        if (nonNull(properties)) {
            return nonNull(properties);
        }
        return false;
    }

    @Override
    public void process(FlowElementProcessContext context) {
        if (!nonNull(context.getModelNode())) {
            return;
        }

//        try {
//            ProcessItem processItem = new ProcessItem();
//            //context.getModelNode().get("modelId").textValue()
//            processItem.setDeployId(context.getDeployId());//部署id
//            processItem.setProcessInstanceName(context.getModelNode().get("properties").get("name").textValue());//流程名称
//            processItem.setProcessInstanceKey(context.getModelNode().get("properties").get("process_id").textValue());//流程编码
//            MqMessageContext messageContext = MqMessageContext.builder().payload(MqPayload.make(processItem))
//                    .tag(MqProcessTypeEnum.DEPLOY.getType())
//                    .messageKey(context.getDeployId())
//                    .build();
//            //这里可以走异步，前面拼接参数还是得同步
//            doSendMessage(messageContext);
//        } catch (Throwable e) {
//            log.error("build messageContext error:",e);
//        }

    }


    private void doSendMessage(MqMessageContext messageContext) {
        MqProducer producer = SpringUtils.getBean(MqProducer.class);
        producer.sendMessage(messageContext);
    }
}
